package com.qys.livesMall.anchor.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qys.livesMall.anchor.mapper.AnchorLivesRoomMapper;
import com.qys.livesMall.anchor.model.dto.lives.sys.LiveRoomBanDateDTO;
import com.qys.livesMall.anchor.model.dto.lives.sys.LiveRoomBanSaveDTO;
import com.qys.livesMall.anchor.model.entity.AnchorLivesRoom;
import com.qys.livesMall.anchor.model.entity.AnchorLivesRoomBan;
import com.qys.livesMall.anchor.mapper.AnchorLivesRoomBanMapper;
import com.qys.livesMall.anchor.service.AnchorLivesRoomBanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.anchor.service.AnchorLivesRoomService;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 主播封禁表 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-06-26
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AnchorLivesRoomBanServiceImpl extends ServiceImpl<AnchorLivesRoomBanMapper, AnchorLivesRoomBan> implements AnchorLivesRoomBanService {


    private final AnchorLivesRoomMapper livesRoomMapper;

    /**
     * 封禁直播间
     *
     * @param saveDTO
     * @return 是否封禁成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void banLiveRoom(LiveRoomBanSaveDTO saveDTO) {
        if (checkLivesBan(saveDTO.getLivesRoomId())!=null){
            throw new QysException(MessageUtils.message("当前直播间已被封禁"));
        }
        //根据直播间查询主播
        AnchorLivesRoom anchorLivesRoom = livesRoomMapper.selectById(saveDTO.getLivesRoomId());
        if (null==anchorLivesRoom){
            throw new QysException("请检查直播间Id");
        }
        anchorLivesRoom.setStatus((byte) 1);
        livesRoomMapper.updateById(anchorLivesRoom);

        AnchorLivesRoomBan ban=new AnchorLivesRoomBan();
        ban.setBanBy(SecurityUtils.getUsername());
        ban.setBanTime(LocalDateTime.now());
        ban.setAnchorId(anchorLivesRoom.getAnchorId());
        ban.setAnchorName(anchorLivesRoom.getAnchorName());
        ban.setActive((byte) 0);
        ban.setLimitRemark(saveDTO.getLimitRemark());
        ban.initBanDate(saveDTO.getBanDay());
        ban.setRealUnbanDate(null);
        baseMapper.insert(ban);
    }

    /**
     * 解封直播间
     *
     * @param livesRoomId 直播间Id
     * @return 是否封禁成功
     */
    @Override
    public void unBanLiveRoom(Long livesRoomId) {
        AnchorLivesRoomBan livesRoomBan = checkLivesBan(livesRoomId);
        if (livesRoomBan==null){
            throw new QysException(MessageUtils.message("当前直播间未被封禁"));
        }
        livesRoomBan.setRealUnbanDate(LocalDate.now());
        livesRoomBan.setActive((byte) 1);
        baseMapper.updateById(livesRoomBan);

        AnchorLivesRoom anchorLivesRoom = livesRoomMapper.selectById(livesRoomId);
        if (null==anchorLivesRoom){
            throw new QysException("请检查直播间Id");
        }
        anchorLivesRoom.setStatus((byte) 0);
        livesRoomMapper.updateById(anchorLivesRoom);
    }

    /**
     * 修改解封时间
     * @param dateDTO
     * @return 是否封禁成功
     */
    @Override
    public void updateBanDate(LiveRoomBanDateDTO dateDTO) {
        AnchorLivesRoomBan livesRoomBan = checkLivesBan(dateDTO.getLivesRoomId());
        if (livesRoomBan==null){
            throw new QysException(MessageUtils.message("未封禁,不能修改增减封禁时间"));
        }
        if (dateDTO.getForeverFlag()){
            livesRoomBan.initBanDate(-1);
        }else {
            livesRoomBan.setBanDay(livesRoomBan.getBanDay()+dateDTO.getChangeDay());
            livesRoomBan.setUnbanDate(livesRoomBan.getUnbanDate().plusDays(dateDTO.getChangeDay()));
            if (livesRoomBan.getUnbanDate().isBefore(LocalDate.now())){
                throw new QysException(MessageUtils.message("修改时间错误"));
            }
        }
        baseMapper.updateById(livesRoomBan);
    }

    /***判断直播间是否已经被封禁*/
    private AnchorLivesRoomBan checkLivesBan(Long livesRoomId) {
        return baseMapper.selectOne(Wrappers.<AnchorLivesRoomBan>lambdaQuery()
                .eq(AnchorLivesRoomBan::getLivesRoomId, livesRoomId)
                .eq(AnchorLivesRoomBan::getActive, 0)
                .last("limit 1")
        );
    }


}
